import { requestUrl } from "@/api/data";
import { deepClone, handleEvent, handleStyle } from "./index"
export const comMixin = {
  props: {
    styleObj: {
      type: Object,
      default: () => { }
    },
    // 父级字体样式
    font: {
      type: Object,
      default: () => { }
    },
    // 用于保存接口相关数据
    dataObject: {
      type: Object,
      default: () => { }
    }
  },
  data() {
    return { resData: [], loading: false };
  },
  computed: {
    dataType() {
      return this.dataObject?.type;
    },

    newStyle() {
      return handleStyle(this.styleObj, this.font)
    },
    attrs() {
      const attrs = deepClone(this.$attrs)
      delete attrs.styleObj // 需要删除attrs中的样式属性，避免传给子级
      return attrs
    },
    clearable() {
      return this.$attrs.clearable && this.value && !this.$attrs.onlyShow && !this.$attrs.disabled && !this.$attrs.readonly
    },
    props() {
      return this.dataObject.props || {}
    }
  },
  created() {
    handleEvent('onCreated', "", this.$attrs, this)
  },
  mounted() {
    handleEvent('onMounted', "", this.$attrs, this)
    this.init()
  },
  watch: {
    dataType(newValue) {
      this.init()
    }
  },
  methods: {
    // 点击事件
    onClick() {
      handleEvent('onClick', "", this.$attrs, this)
    },
    // 在 Input 失去焦点时触发
    onBlur(value) {
      handleEvent('onBlur', value, this.$attrs, this)
    },
    // 在 Input 获得焦点时触发
    onFocus(value) {
      handleEvent('onFocus', value, this.$attrs, this)
    },
    // 在 Input 值改变时触发
    onInput(value) {
      handleEvent('onInput', value, this.$attrs, this)
    },
    // 点击清空按钮时触发
    onClear(value) {
      handleEvent('onClear', value, this.$attrs, this)
    },
    // 绑定值或选中项变化时触发
    onChange(value) {
      handleEvent('onChange', value, this.$attrs, this)
    },
    // 点击确认按钮时触发
    onConfirm(value) {
      handleEvent('onConfirm', value, this.$attrs, this)
    },
    init() {
      if (!this.dataType) return
      this.resData = [];
      if (this.dataType == "static") {
        this.resData = this.dataObject.value;
      } else if (this.dataType == "dynamic") {
        this.loadData();
      }
    },
    loadData() {
      console.log("111111111111")
      requestUrl(this.dataObject.url, this.dataObject.method, this.dataObject.request)
        .then(
          (res) => {
            this.resData = res.result;
            if (this.dataObject.response) {
              const func = new Function("response", this.dataObject.response);
              func(res.result)
            }
          }
        ).catch((err) => {
          if (this.data.error) {
            const func = new Function("error", this.data.error);
            func(err);
          }
        });
      // const url = this.$attrs.__config__.url;
      // const method = this.$attrs.__config__.method;
      // if (!url || !method) return;
      // requestUrl(url, method)
      //   .then(rsp => {
      //     this.loading = false;
      //     this.resData = rsp;
      //   })
      //   .catch(err => {
      //     this.loading = false;
      //     this.$message.error(err.response.result);
      //   });
    }
  }
};